/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package org.sbbs.admin.mbean.sysarea;

import jakarta.annotation.PostConstruct;
import jakarta.faces.application.FacesMessage;
import static jakarta.faces.application.FacesMessage.SEVERITY_ERROR;
import static jakarta.faces.application.FacesMessage.SEVERITY_WARN;
import jakarta.faces.context.FacesContext;
//import jakarta.faces.view.ViewScoped;
//import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.omnifaces.cdi.ViewScoped;
import org.primefaces.PrimeFaces;
import org.primefaces.component.tree.TreeDragDropInfo;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.TreeDragDropEvent;
import org.primefaces.model.DialogFrameworkOptions;
//import org.omnifaces.cdi.ViewScoped;
import org.primefaces.model.LazyDefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.sbbs.admin.dao.SysAreaRepo;
import org.sbbs.admin.entity.SysArea;
import org.sbbs.admin.entity.SysOrg;
import org.sbbs.admin.manager.SysAreaManager;
import static org.sbbs.admin.manager.SysAreaManager.rootId;
import org.sbbs.admin.manager.SysOrgManager;
import org.sbbs.admin.mbean.sysorg.SysOrgMBean;
import static org.sbbs.base.jsf.BaseTableMBean.addDetailMessage;
import org.sbbs.base.jsf.BaseTreeMBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.MessageSourceAccessor;

/**
 *
 * @author jenkins
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Named("sysAreaMBean")
@ViewScoped
public class SysAreaMBean  extends BaseTreeMBean<SysArea, Long, SysAreaRepo, SysAreaManager> {

    private static final long serialVersionUID = 5059124868263941246L;

//    private static SysAreaManager manager;
     private static SysAreaManager staticManager;

    public SysAreaMBean(SysAreaManager m) {
        super(m);
        SysAreaMBean.staticManager = m;
    }

    @PostConstruct
    public void init() {
        this.setRoot(this.initRoot());
    }

    @Override
    protected TreeNode initNode(SysArea r) {
        TreeNode<SysArea> n = new LazyDefaultTreeNode<>(r,
                (s) -> staticManager.findChildren(s),
                (s) -> s.isLeaf());
        return n;
    }

////    private TreeNode<SysArea> root;
////    private List<SysArea> childrens;
//////    private TreeNode<SysArea> root;
////
////    private SysArea editObj;
//
//    public SysAreaMBean(SysAreaManager m) {
//        SysAreaMBean.manager = m;
//    }
//
//    @PostConstruct
//    public void init() {
//        root = this.initRoot();
//    }
//
//    private TreeNode initRoot() {
//        SysArea r = manager.findById(SysAreaManager.rootId);
////        r.setHasChildren(true);
//
////        root = new LazyDefaultTreeNode<>(r, null, null);
////        root = new LazyDefaultTreeNode<>(r,
////                (s) -> manager.findChildren(s),
////                (s) -> s.isLeaf());
//        root = this.initNode(r);
//        return root;
//    }
//
//    private TreeNode initNode(SysArea r) {
//        TreeNode<SysArea> n = new LazyDefaultTreeNode<>(r,
//                (s) -> manager.findChildren(s),
//                (s) -> s.isLeaf());
//
//        return n;
//    }
//
//    private TreeNode<SysArea> selectedNode;
//    private String selectedNodeRowKey;
//
//    public void displaySelectedSingle() {
//        if (selectedNode != null) {
//            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedNode.getData().toString());
//            FacesContext.getCurrentInstance().addMessage(null, message);
//        }
//    }
//
//    private TreeNode[] selectedNodes2;
//
//    public void displaySelectedMultiple(TreeNode[] nodes) {
//        if (nodes != null && nodes.length > 0) {
//            StringBuilder builder = new StringBuilder();
//
//            for (TreeNode node : nodes) {
//                builder.append(node.getData().toString());
//                builder.append("<br />");
//            }
//
//            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", builder.toString());
//            FacesContext.getCurrentInstance().addMessage(null, message);
//        }
//    }
//
////    public void onNodeExpand(NodeExpandEvent event) {
////        SysArea obj = (SysArea) event.getTreeNode().getData();
////        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Expanded", obj.getName());
////        FacesContext.getCurrentInstance().addMessage(null, message);
////    }
////
////    public void onNodeCollapse(NodeCollapseEvent event) {
////          SysArea obj = (SysArea) event.getTreeNode().getData();
////        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Collapsed", obj.getName());
////        FacesContext.getCurrentInstance().addMessage(null, message);
////    }
//    public void onNodeSelect(NodeSelectEvent event) {
//
//        SysArea obj = (SysArea) event.getTreeNode().getData();
//        this.editObj = obj;
//        this.childrens = manager.findChildren(obj);
//        event.getTreeNode().getChildren();
//        selectedNodeRowKey = event.getTreeNode().getRowKey();
//
//        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", selectedNodeRowKey);
////        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", obj.getName());
//        FacesContext.getCurrentInstance().addMessage(null, message);
//    }
//
////    public void onNodeUnselect(NodeUnselectEvent event) {
////          SysArea obj = (SysArea) event.getTreeNode().getData();
////        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Unselected", obj.getName());
////        FacesContext.getCurrentInstance().addMessage(null, message);
////    }
//    public void newOpt(String form) {
//        this.editObj = new SysArea();
//        this.editObj.setPid(rootId);
//        this.editObj.setParent(this.root.getData());
//        this.editObj.setLevel(this.root.getData().getLevel());
//        this.openDialog(form);
//    }
//
//    public void editOpt(String form) {
//        this.openDialog(form);
//    }
//
//    public void sortChildrens(String form) {
//        this.openDialog(form);
//    }
//
//    public void openDialog(String form) {
////        this.editDict = this.selectedSysDict;
//        String outcome = null;
//        DialogFrameworkOptions options = DialogFrameworkOptions.builder()
//                .resizable(true)
//                .draggable(true)
//                .modal(true)
//                .closeOnEscape(true)
//                .maximizable(true)
//                .minimizable(true)
//                .fitViewport(true)
//                .responsive(true)
//                .blockScroll(false)
//                // “explode”（爆炸效果）、“slide”（滑动效果）、“fade”
//                .showEffect("fade")
//                .hideEffect("explode")
//                .contentWidth("1600")
//                .contentHeight("800")
//                .build();
//        PrimeFaces.current().dialog().openDynamic(form, options, null);
//    }
//
//    public void closeDialog() {
////        PrimeFaces.current().dialog().closeDynamic(null);
//        PrimeFaces.current().dialog().closeDynamic(this.editObj);
//    }
//
//    public void save() {
//        try {
////            editObj.setPid(editObj.getParent().getId());
////            editObj.setParent(null);
//            manager.save(editObj);
//            this.initRoot();
//            PrimeFaces.current().dialog().closeDynamic(editObj);
//        } catch (Exception e) {
//            e.printStackTrace();
//            addDetailMessage(e.getMessage(), SEVERITY_ERROR);
//
//        }
//    }
//    @Autowired
//    @Qualifier("sma")
//    //这个类的定义方式是个学问呀 @important
//    private MessageSourceAccessor sma;
//
//    public void delSingle() {
//        if (this.editObj != null && !this.editObj.isHasChildren()) {
//            try {
//                manager.remove(editObj);
////                Object[] args = {singleSelected.getId()};
//                this.initRoot();
//                addDetailMessage(sma.getMessage("delete.success"));
//            } catch (Exception e) {
//                e.printStackTrace();
//                addDetailMessage(sma.getMessage("delete.exception"), SEVERITY_ERROR);
//            }
//        } else {
//            addDetailMessage(sma.getMessage("delete.noselected"), SEVERITY_WARN);
//        }
//    }
//
//    public void onDragDrop(TreeDragDropEvent event) {
//        TreeNode dragNode = event.getDragNode();
//        TreeNode dropNode = event.getDropNode();
//        int dropIndex = event.getDropIndex();
//
//        SysArea drop = (SysArea) dropNode.getData();
//        SysArea drag = (SysArea) dragNode.getData();
//        drag.setPid(drop.getId());
//        drag.setParent(drop);
//        manager.save(drag);
//
//    }
//
//    public boolean onDrop(TreeDragDropInfo event) {
//        TreeNode dragNode = event.getDragNode();
//        TreeNode dropNode = event.getDropNode();
//        int dropIndex = event.getDropIndex();
//
//        SysArea drop = (SysArea) dropNode.getData();
//        SysArea drag = (SysArea) dragNode.getData();
//
//        if (drop.isLeaf()) {
//            // return true if info.getDragNode() or info.getDragNodes() can be dropped on info.getDropNode()
//            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN,
//                    "拖拽失败 ", "拖拽目标为叶子,无法添加子节点");
//            FacesContext.getCurrentInstance().addMessage(null, message);
//            return false;
//        } else {
//            return true;
//        }
//    }
//
//    public void onReorder() {
//        FacesContext context = FacesContext.getCurrentInstance();
//        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "List Reordered", null));
//    }
//
//    public void onNodeExpand(NodeExpandEvent event) {
//        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Expanded", event.getTreeNode().toString());
//        FacesContext.getCurrentInstance().addMessage(null, message);
//    }
//
//    public void saveSortChange() {
//        manager.updateChildrenSort(childrens);
//        this.initRoot();
//        this.root.setExpanded(true);
//    }
//    public void onRowReorder(ReorderEvent event) {
//        manager.updateChildrenSort(childrens);
//        this.initRoot();
//        this.root.setExpanded(true);
//
////        this.selectedNode.setExpanded(true);
////        TreeNode p = this.selectedNode.getParent();
////
////        this.selectedNode = this.initNode(this.selectedNode.getData());
////        this.selectedNode.setParent(p);
////String rk=this.selectedNode.getRowKey();
////String ty=this.selectedNode.getType();
////        this.selectedNode.setRowKey(rk);
////        List<TreeNode<SysArea>> tc=this.selectedNode.getChildren();
////        for (int i = 0; i < tc.size(); i++) {
////            TreeNode<SysArea> n=tc.get(i);
////            n.
////        }
////      ( (LazyDefaultTreeNode) this.selectedNode).igetChildren();
////        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Row Moved", "From: " + event.getFromIndex() + ", To:" + event.getToIndex());
////        FacesContext.getCurrentInstance().addMessage(null, msg);
//    }


}
